<template>
  <app-section>
    <van-nav-bar :title="navTitle" @click-left="backRoute" left-arrow/>
    <van-pull-refresh v-model="pullLoading" @refresh="onRefresh">
      <van-list
        v-model="listLoading"
        :finished="listFinished"
        :finished-text="finishedText"
        :error.sync="listError"
        error-text="请求失败，点击重新加载"
        @load="onLoad()">

        <van-cell
          icon="static/mobile/icon/user.png"
          v-for="(item,index) in applyList"
          :key="index"
          :title="item.bpmBizTitle"
          is-link
          @click="taskDetailRoute(item)">
          <template slot="label">
            <van-tag plain type="primary">{{ item.processDefinitionName }}</van-tag>
            <div style="padding-top:3px">
              {{ item.processApplyUserId }}
              <span style="margin-left:5px">{{ item.taskBeginTime }}</span>
            </div>
          </template>
          <div slot="right-icon" v-if="!item.taskAssigneeName || item.taskAssigneeName==''">
            <van-tag plain type="warning">待签收</van-tag>
          </div>
        </van-cell>

      </van-list>
    </van-pull-refresh>
  </app-section>
</template>

<script>
  import AppBlock from '@/components/AppBlock'
  import AppSection from '@/components/AppSection'
  import { getAction,putAction } from '@/api'

  export default {
    name: "tab-wait",
    components:{
      AppSection,AppBlock
    },
    data() {
      return {
        navTitle:"待审批",
        listError:false,
        listLoading: false,
        pullLoading: false,
        listFinished:false,
        finishedText:'没有更多了',
        pageNo:1,
        pageSize:5,
        url:"/act/task/list",
        claimUrl:'/act/task/claim',
        applyList:[]
      }
    },
    mounted(){

    },
    watch: {
      '$route' () {
        this.onRefresh()
      }
    },
    methods: {
      taskDetailRoute(item){
        //console.log("processInstanceId",processInstanceId)
        if(item.taskAssigneeName&&item.taskAssigneeName!=''){
          this.$router.push({
            name: "myTaskDetail",
            params: {
              taskId:item.id,
              instanceId: item.processInstanceId
            }
          })
        }else{
          //签收逻辑
          this.handleClaim(item)
        }
      },
      handleClaim(record){
        var that = this;
        this.$dialog.confirm({
          title: '提示',
          message:"是否签收该任务?"
        }).then(() => {
          var params = {taskId:record.id};//查询条件
          putAction(that.claimUrl, params).then((res) => {
            if (res.success) {
              that.$toast(res.message);
              that.onRefresh();
            } else {
              that.$toast.fail(res.message);
            }
          })
        }).catch(() => {

        });

      },
      onRefresh() {
        this.pullLoading = true
        this.pageNo = 1
        let param = {
          pageNo: this.pageNo,
          pageSize: this.pageSize
        }
        this.applyList=[]
        getAction(this.url,param).then(res=>{
          this.pullLoading = false
          if(res.success){
            this.pageNo++
            let arr = res.result.records
            for(let item of arr){
              this.applyList.push(item)
            }
          }
        })
      },
      onLoad(){
        this.listLoading = true;
        let param = {
          pageNo: this.pageNo,
          pageSize: this.pageSize
        }
        console.log("请求参数",param)
        getAction(this.url,param).then(res=>{
          this.listLoading = false;
          console.log("请求结果",res)
          if(res.success){
            // this.$toast.success("加载完成");
            this.pageNo++
            let arr = res.result.records
            for(let item of arr){
              this.applyList.push(item)
            }

            if(!arr || arr.length<this.pageSize){
              console.log("加载完成!")
              this.listFinished = true;
            }
           // console.log("结果数据", this.applyList)
          }else{
            this.error = true;
          }
        }).catch(e=>{
          console.log("请求错误",e)
          this.error = true;
        })
      },
        backRoute(){
            this.$router.go(-1);//返回上一层
        },
    },

  }
</script>

<style scoped lang="less">
  .demo-pull-refresh {
    background-color:#fff;

    .van-pull-refresh {
      &,
      &__track {
        height: calc(100vh - 50px);
      }
    }

  }
</style>
